//
//  PhotoModelDataCache.swift
//  FileManager-NSCache-HttpDownload
//
//  Created by DaleGuo on 2023/9/19.
//

import Foundation
import SwiftUI

// Cache逻辑，其中接口方法和FileManager要类似
class PhotoModelDataCacheManager {
    static let instance = PhotoModelDataCacheManager()
    private init() {}

    var photoCache: NSCache<AnyObject, AnyObject> = {
        var cache = NSCache<AnyObject, AnyObject>()
        cache.countLimit = 50
        cache.totalCostLimit = 1024 * 1024 * 100

        return cache
    }() // 这里写法是不是有点像匿名方法

    func add(key: String, value: UIImage) {
        photoCache.setObject(value, forKey: key as NSString)
    }

    func get(key: String) -> UIImage? {
        return photoCache.object(forKey: key as NSString) as? UIImage
    }
}
